package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.constant.CommonConstant;
import com.kgc.scd.service.CollectServerService;
import com.kgc.scd.service.CollectWebService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.vo.NideshopCollectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created On : 2022/11/14.
 * <p>
 * Author : yantao
 * <p>
 * Description: CollectController
 */
@Slf4j
@RestController
@RequestMapping("/yt")
@Api(tags = "我的收藏功能")
public class CollectController {


    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private CollectWebService collectWebService;

    @Autowired
    private CollectServerService collectServerService;

    /**
     * @author : yantao
     * @date   : 2022/11/18
     * @param  : [pageNo, pageSize, name, shopUser]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 收藏列表所有
     */
    @GetMapping("selectCollect")
    @TokenPermission
    @ApiOperation(value = "收藏列表所有",notes = "有参,默认每页6个,最多收藏20个,支持分页,统一返回,mybatis-plus技术")
    public RequestResult<?> selectCollect(@ApiParam(value = "页码") @RequestParam(defaultValue = CommonConstant.DEFAULT_INIT_PAGE_NO) Integer pageNo,
                                          @ApiParam(value = "每页数量") @RequestParam(defaultValue = CommonConstant.DEFAULT_INIT_PAGE_SIZE) Integer pageSize,
                                          @RequestParam String name,
                                          @ApiIgnore @RequestShopUser ShopUser shopUser
                                          ){

        log.info("------  web controller 调用 数据,{},{}",pageNo,pageSize);


        //shopUser.getId();
        PageSupport<NideshopCollectVO> pageSupport = collectServerService.getCollectAll(pageNo,pageSize,name,shopUser.getId());
        log.info("------  web controller 返回 数据,{}",pageSupport);
        return ResultBuildUtil.success( pageSupport );
    }

    /**
     * @author : yantao
     * @date   : 2022/11/18
     * @param  : [shopUser, goodsId]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 进行收藏,有参,用户id,商品id,5秒内收最多收藏5个
     */
    @GetMapping("doCollectAndCancel")
    @TokenPermission
    @ApiOperation(value = "进行收藏",notes = "有参,用户id,商品id,5秒内收最多收藏5个,统一返回,redis,mybatis-plus技术")
    public RequestResult<?> doCollectAndCancel( @ApiIgnore @RequestShopUser ShopUser shopUser,
                                                @ApiParam(value = "商品id") @RequestParam String goodsId
    ){
        String userId = shopUser.getId();

        log.info("------  web controller 进行收藏,{},{}",userId,goodsId);
        collectWebService.isAlreadyCollected(userId);



        if (collectWebService.isExceedLimit(userId)){
            return ResultBuildUtil.fail( "101","收藏夹已满,清清理收藏夹" );
        }

        if (collectWebService.testIsExceedNum(userCollectConfig.userCollectLimitClickPrefix,userId)){
            return ResultBuildUtil.fail( "102","收藏过快,请等一下尝试" );
        }

        /**
         * 是否已经收藏,true已经收藏,false没有收藏
         */
        if (collectServerService.isCollect(userId,goodsId)){
            //取消收藏,(删除)
            if (collectServerService.cancelCollection(userId,goodsId)){
                collectWebService.cancelCollectionRedis(userId);
                return ResultBuildUtil.success( "已取消收藏" );
            }

        }else {
            //进行收藏
            if (collectServerService.addCollection(userId,goodsId)){
                collectWebService.doCollectRedis(userId);
                return ResultBuildUtil.success( "已加入收藏" );
            }


        }



        

        return ResultBuildUtil.fail("201","未知错误");
    }

    /**
     * @author : yantao
     * @date   : 2022/11/18
     * @param  : [shopUser, goodsId]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 进行删除
     */
    @GetMapping("/removeColllect")
    @TokenPermission
    @ApiOperation(value = "进行删除",notes = "有参,用户id,商品id,统一返回,redis,mybatis-plus技术")
    public RequestResult<?> removeColllect(@ApiIgnore @RequestShopUser ShopUser shopUser,
                                           @ApiParam(value = "商品id") @RequestParam String goodsId
    ){

        String userId = shopUser.getId();
        if (collectServerService.cancelCollection(userId,goodsId)){
            collectWebService.cancelCollectionRedis(userId);
            return ResultBuildUtil.success( "已删除收藏" );
        }
        return ResultBuildUtil.fail( "205","删除收藏失败" );
    }





}
